package com.zmkj.cn.comm.util;

import com.zmkj.cn.comm.baseenum.SeparatorEnum;
import org.apache.tomcat.util.codec.binary.Base64;
/**
 * @author 伍绍宇 Coy
 */
public class Base64Util {

    public static String byteToStringBase64(byte[] b) {
        String base64Data = "";
        try {
            base64Data = Base64.encodeBase64String(b);
            if ((base64Data != null) && (base64Data != "")) {
                base64Data = base64Data.replaceAll("\\+", "*").replaceAll("\\/", "-");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return base64Data;
    }

    public static byte[] base64StringToByte(String base64String) {
        if (base64String == null) {
            return null;
        }
        base64String = base64String.replaceAll("\\*", "+").replaceAll("-", SeparatorEnum.SLASH.code);

        byte[] b = null;
        b = Base64.decodeBase64(base64String);
        for (int i = 0; i < b.length; i++) {
            if (b[i] < 0) {
                b[i] = ((byte) (b[i] + 256));
            }
        }
        return b;
    }
}
